﻿var Aspfm = new Object(); Aspfm.Data = new Object(); Aspfm.Data.Form = new Object(); Aspfm.Data.Global = new Object(); Aspfm.Data.Url = new Object();
var Aspfmfun = new Object(); var AspfmComputed = new Object();
var Aspfm_lasttime = true; var fullloading;
function serverurl() {
    return _path + "AspfmAshx.ashx";//"http://127.0.0.1:8090/Aspfm.Server/Aspfm";//
}

(function () {
    document.onreadystatechange = function () {
        NProgress.start();
        if (document.readyState == "Uninitialized") {
            NProgress.set(1);
        }
        if (document.readyState == "Interactive") {
            NProgress.set(0.5);
        }
        if (document.readyState == "complete") {
            NProgress.done();
        }
    }
})();
function ShowMsg(msg) {
    vm.$alert(msg, '提示', { confirmButtonText: '确定' });
}
//注册String的startsWith方法，因为有些浏览器可能不支持startsWith
String.prototype.startsWith = function (s) {
    if (s == null || s == "" || this.length == 0 || s.length > this.length)
        return false;
    return (this.substr(0, s.length) == s);
}
String.prototype.Trim = function () {
    return this.replace(/(^\s*)|(\s*$)/g, "");//去首尾空格
}
//获取URL参数
function getUrlStr(name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
    var r = window.location.search.substr(1).match(reg);
    if (r != null) return unescape(r[2]);
    return '';
}
function getPkSequence(sequencename, connkey) {
    if (sequencename) {
        var Sequence = new Object();
        Sequence.sequencename = sequencename;
        Sequence.connkey = connkey;
        var defer = $.Deferred();
        $.when(ajaxPsotActive("Aspfm.Html", "Aspfm.Html.AspfmWeb", "pksequence", Sequence)).done(function (resdata) {
            defer.resolve(resdata.BWebResult[0].WebDataSet[0].Rows[0].PK);
        });
        return defer;
    }
}
function getPkId() {
    var defer = $.Deferred();
    $.when(GetServiceTime(true)).done(function (resdata) {
        defer.resolve(resdata + GuidS4() + GuidS4());
    });
    return defer;
}
//前缀，根据前缀不一样，计算唯一的流水码。
//数据缓存在ASPFM_PK_INT_MAX表中。
//LASTPKID ：最后一次取号的对象。如果最后一次取号的对象不变，则OrderId不变。
function GetPkOrderId(beforeStr, length, LASTPKID, connkey) {
    if (beforeStr) {
        var PkOrder = new Object();
        PkOrder.beforeStr = beforeStr;
        PkOrder.LASTPKID = LASTPKID;
        PkOrder.connkey = connkey;
        PkOrder.length = length;
        var defer = $.Deferred();
        $.when(ajaxPsotActive("Aspfm.Html", "Aspfm.Html.AspfmWeb", "GetPkOrderId", PkOrder)).done(function (resdata) {
            defer.resolve(resdata.BWebResult[0].Result.Values.PkOrderId);
        });
        return defer;
    }
}
function isRealNum(val) {
    if (val) {
        // isNaN()函数 把空串 空格 以及NUll 按照0来处理 所以先去除
        if (val === "" || val == null) {
            return false;
        }
        if (!isNaN(val)) {
            return true;
        } else {
            return false;
        }
    }
    return false;
}
function GetServiceTime(isnumber) {
    Aspfm_lasttime = false;
    var timeDeferred = ajaxPsot(serverurl() + "?isnumber=" + isnumber);
    Aspfm_lasttime = true;
    return timeDeferred;
}
function GetDateTimeJs(isnumber) {
    var date = new Date();
    var year = date.getFullYear();
    var month = date.getMonth() + 1;
    var day = date.getDate();
    var hour = date.getHours();
    var minute = date.getMinutes();
    var second = date.getSeconds();
    month = month < 10 ? "0" + month : month;
    hour = hour < 10 ? "0" + hour : hour;
    day = day < 10 ? "0" + day : day;
    minute = minute < 10 ? "0" + minute : minute;
    second = second < 10 ? "0" + second : second;
    if (isnumber == true) {
        return year + '' + month + '' + day + '' + hour + '' + minute + '' + second;
    }
    return year + '-' + month + '-' + day + ' ' + hour + ':' + minute + ':' + second;
}
function GuidS4() {
    return (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1).toUpperCase();
}
function CheckLoginTime() {
    if ((typeof localStorage)=="object" && window.localStorage) {
        $.when(GetServiceTime(true)).done(function (resdata) {
            var Aspfm_timenow = resdata;
            var Aspfm_timenow = vdata;
            var Aspfm_lasttime = window.localStorage.getItem("Aspfm_lasttime");
            var temptime = Aspfm_timenow - Aspfm_lasttime;
            if (temptime > 3000) {//3000表示30分钟,如果30分钟没有调用过后台，就要求重新登录。
                $.when(ajaxPsotActive("Aspfm.Login", "Aspfm.Login.Login", "Logout")).done(function (resdata) {
                    window.location.href = _path + "login.aspx";
                });
            }
        });
    }
}
function ajaxPsot(url, params) {
    Aspfm.Data.fmparams = params;
    //对html转码，防止恶意输入
    for (var tempKey in Aspfm.Data.Form) {
        if (Aspfm.Data.Form[tempKey] === Aspfm.Data.Form[tempKey] + '' && Aspfm.Data.Form[tempKey].indexOf('<') >= 0) {
            Aspfm.Data.Form[tempKey] = HtmlUtil.htmlEncodeByRegExp(Aspfm.Data.Form[tempKey]);
        }
    }
    var ajaxDeferred = ajaxPsotFull(url, Aspfm.Data);
    //对html解码，在防止恶意输入之后，解码显示
    for (var tempKey in Aspfm.Data.Form) {
        if (Aspfm.Data.Form[tempKey] === Aspfm.Data.Form[tempKey] + '' && Aspfm.Data.Form[tempKey].indexOf("&lt;") >= 0) {
            Aspfm.Data.Form[tempKey] = HtmlUtil.htmlDecodeByRegExp(Aspfm.Data.Form[tempKey]);
        }
    }
    return ajaxDeferred;
}
function ajaxPsotFull(url, params) {
    if (Aspfm_lasttime == true && (typeof localStorage)=="object" && window.localStorage) {
        setTimeout(function () { window.localStorage.setItem("Aspfm_lasttime", GetDateTimeJs(true)); }, 1000);
    }
    var ajaxDeferred = $.ajax({
        type: "post",
        //dataType: 'json',
        ajaxStart: NProgress.start(),
        ajaxStop: NProgress.done(),
        url: encodeURI(url),
        data: params,
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            var errtitle = '服务器内部报错';
            if (XMLHttpRequest.status == 0) {
                errtitle = '无法连接远程服务器';
            }
            else if (XMLHttpRequest.responseJSON) {
                errtitle = XMLHttpRequest.responseJSON.Message;
            }
            vm.$alert(errtitle, "服务器错误-" + XMLHttpRequest.status, {
                confirmButtonText: '错误详情', type: 'error', center: true,
                callback: function (val) {
                    if (val != "cancel") {
                        winopen = window.open('', '', '');
                        winopen.document.write(XMLHttpRequest.responseText);
                    }
                }
            });
        }
    });
    return $.when(ajaxDeferred).done(function (resdata) {
        if (resdata && resdata.Success == false) {
            ShowMsg(resdata.ErrMsg);
        }
    });
}
function ajaxPsotActive(dll, clss, active, params) {
    var fullurl = GetFullUrl(dll, clss, active);
    return ajaxPsot(fullurl, params);
}
function GetFullUrl(dll, clss, active) {
    return serverurl() + "?fmparams.Dll=" + dll + "&fmparams.Class=" + clss + "&fmparams.Active=" + active;
}
function onBwebSave(execsign, refunction, _async) {
    return ajaxPsotActive("Aspfm.Html", "Aspfm.Html.AspfmWeb", "websave", execsign);
}
function onBwebQuery(pageindex, pagesize, sign) {
    var fmpage = new Object();
    fmpage.pageindex = pageindex;
    fmpage.pagesize = pagesize;
    fmpage.execsign = sign;
    return ajaxPsotActive("Aspfm.Html", "Aspfm.Html.AspfmWeb", "querysql", fmpage);
}
function webDownload(fileid) {
    AjaxGetAndOpen("Aspfm.FtpFile", "Aspfm.FtpFile.FtpControl", "read", "fileid=" + fileid);
}
function webTempFile(filename) {
    AjaxGetAndOpen("Aspfm.FtpFile", "Aspfm.FtpFile.FtpControl", "read", "filename=" + filename);
}
function AjaxGetAndOpen(dll, clss, active, urlparams) {
    var fileurl = GetFullUrl(dll, clss, active);
    window.open(fullurl + "&" + urlparams);
}
function webFileSaveUrl(ftpkey) {
    var fileurl = _path + "AspfmAshx.ashx?fmparams.Dll=Aspfm.FtpFile&fmparams.Class=Aspfm.FtpFile.FtpControl&fmparams.Active=save&ftpkey=" + ftpkey;
    return fileurl;
}
function WriteLogSql(msg, objid) {
    var fmlog = new Object();
    fmlog.logmsg = msg;
    fmlog.logobjid = objid;
    return ajaxPsotActive("Aspfm.Html", "Aspfm.Html.AspfmWeb", "WriteLogSql", fmlog);
}
function transDataToTree(a, idStr, pidStr, cnodesStr) {
    if (!cnodesStr) {
        cnodesStr = 'cnodes';
    }
    var r = [], hash = {}, id = idStr, pid = pidStr, cnodes = cnodesStr, i = 0, j = 0, len = a.length;
    for (; i < len; i++) {
        hash[a[i][id]] = a[i];
    }
    for (; j < len; j++) {
        var aVal = a[j], hashVP = hash[aVal[pid]];
        if (hashVP) {
            !hashVP[cnodes] && (hashVP[cnodes] = []);
            if (hashVP[cnodes].indexOf(aVal) == -1) {
                hashVP[cnodes].push(aVal);
            }
        } else {
            r.push(aVal);
        }
    }
    return r;
}
function getWindowTop() {
    var obj = self;
    while (true) {
        if (obj == top) {
            break;
        }
        obj = obj.parent;
    }
    return obj;
}
function showdiv(ShowMenu) {
    var windowtop = getWindowTop();
    windowtop.fullloading = windowtop.Vue.prototype.$loading({
        lock: true,
        text: 'Loading',
        spinner: 'el-icon-loading',
        background: 'rgba(0, 0, 0, 0.7)'
    });
    if (ShowMenu != true) {
        $(windowtop.document).bind("contextmenu", function (e) { return false; });
        $(windowtop.document).bind("click", function (e) { showopenerwindow(); });
    }
}
function hidediv() {
    var windowtop = getWindowTop();
    if (windowtop.fullloading) { windowtop.fullloading.close(); }
    $(windowtop.document).unbind("contextmenu");
    $(windowtop.document).unbind("click");
}
var HtmlUtil = {
    /*3.用正则表达式实现html转码*/
    htmlEncodeByRegExp: function (str) {
        var s = "";
        if (str.length == 0) return "";
        //s = str.replace(/&/g,"&amp;");
        s = str.replace(/</g, "&lt;");
        //s = s.replace(/>/g,"&gt;");
        //s = s.replace(/ /g,"&nbsp;");
        //s = s.replace(/\'/g,"&#39;");
        //s = s.replace(/\"/g,"&quot;");
        return s;
    },
    /*4.用正则表达式实现html解码*/
    htmlDecodeByRegExp: function (str) {
        var s = "";
        if (str.length == 0) return "";
        //s = str.replace(/&amp;/g,"&");
        s = str.replace(/&lt;/g, "<");
        //s = s.replace(/&gt;/g,">");
        //s = s.replace(/&nbsp;/g," ");
        //s = s.replace(/&#39;/g,"\'");
        //s = s.replace(/&quot;/g,"\"");
        return s;
    }
};
function CreatePageImg() {
    html2canvas(document.querySelector("#formdesign_Aspfm")).then(function (canvas) {
        //document.body.appendChild(canvas);
        var vimg = canvas.toDataURL();
        var vimgname = document.title + ".png";
        vm.$alert('<a href="' + vimg + '" download="' + vimgname + '"><img style="vertical-align:middle;width:100%" src="' + vimg + '" title="点击保存"></a>', vimgname, {
            dangerouslyUseHTMLString: true,
            showConfirmButton: false
        });
    });
}
function execWinForm(eventname,eventData)
{
	var htmldata=objtoJsonHtml(eventData);
	if(window.external)
    {
		var WinForm =window.external.RaiseEvent(eventname,htmldata);
		var objWinForm=JSON.parse(WinForm);
		return objWinForm;
	}
	else if(window.boundEvent)
	{
		var WinForm =window.boundEvent.RaiseEvent(eventname,htmldata);
        var objWinForm=JSON.parse(WinForm);
        return objWinForm;
	}
}
function objtoJsonHtml(eventData)
{
	var htmldata='';
	if(eventData)
	{
		if((typeof eventData)=="object")
		{
			htmldata=JSON.stringify(eventData);
		}
		else
		{
			htmldata=eventData;
		}
	}
	return htmldata;
}
function returnWinForm(eventData)
{
	if(window.external)
    {
		var htmldata=objtoJsonHtml(eventData);
		var aspfmWinForm =  document.getElementById('aspfmWinForm');
		if(aspfmWinForm)
		{
			aspfmWinForm.value=htmldata;
		}
		else
		{
			var div = document.createElement("div");
			div.innerHTML = "<input type='hidden' value='"+htmldata+"' id='aspfmWinForm'></input>";
			document.body.appendChild(div);
		}
	}
	return eventData;
}